//
//  SYComments.swift
//  SingWeibo
//
//  Created by shiying on 15/12/25.
//  Copyright © 2015年 shiying. All rights reserved.
//

import UIKit

class SYComments: NSObject {

    var id: Int? /// 查询微博的ID
    
    var text: String? /// 评论的内容
    
    var user: SYUser?
    
    var rowHeight: CGFloat?
    
    /// KVC
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
        
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String: AnyObject] {
                
                let dictModel = SYUser(dict: dict)
                self.user = dictModel
                
            }
            return// 必须return，不然会调用父类的方法
        }
        super.setValue(value, forKey: key)
    }
    
    override var description: String {
        let prop = ["id", "text", "user", "status"]
        let dict = dictionaryWithValuesForKeys(prop)
        return "评论模型 \(dict)"
    }
    
    //MARK： - 评论数据
    class func loadStatusCommentsModel(id: Int, finished: (comments: [SYComments]? , error: NSError?) ->()) {
        
        SYAFNTool.sharedInsetace.loadComments(id) { (result, error) -> () in
            
            if error != nil || result == nil {
                finished(comments: nil, error: error)
                return
            }
            
            if let array =  result!["comments"] as? [[String : AnyObject]] {
                
                //定义可变数组
                var comments = [SYComments]()
                
                for dict in array {
                    let comment = SYComments(dict: dict)
                    comments.append(comment)
                    
                }
                finished(comments: comments, error: nil)
                
            } else {
                finished(comments: nil, error: nil)
            }
        }
    }

    
}
